package com.skyolder.crutch.word.token.raw;

import com.skyolder.crutch.error.GrammarError;
import com.skyolder.crutch.word.raw.BufferCharPiper;
import com.skyolder.crutch.word.raw.BufferCharQueue;
import com.skyolder.crutch.word.types.RawTokenType;

public class NotesTokenCreator implements TokenCreator {
	public RawToken tryCreateToken(BufferCharQueue bufferCharQueue) throws GrammarError {
		char c = bufferCharQueue.peekChar();
		if (c != '/') {
			return null;
		}
		char cc = bufferCharQueue.peekChar();
		if (cc != '/' && cc != '*') {
			return null;
		}
		if (cc == '/') {
			bufferCharQueue.nextChar();
			bufferCharQueue.nextChar();
			StringBuilder stringBuilder = new StringBuilder();
			while (true) {
				char ccc = bufferCharQueue.nextChar();
				if (ccc == '\r') {
					char cccc = bufferCharQueue.peekChar();
					if (cccc == '\n') {
						bufferCharQueue.nextChar();
					}
					return new RawToken(stringBuilder.toString(), RawTokenType.NOTES);
				} else if (ccc == '\n') {
					return new RawToken(stringBuilder.toString(), RawTokenType.NOTES);
				}
				stringBuilder.append(ccc);
			}
		} else {
			bufferCharQueue.nextChar();
			bufferCharQueue.nextChar();
			StringBuilder stringBuilder = new StringBuilder();
			while (true) {
				char ccc = bufferCharQueue.nextChar();
				if (ccc == '*') {
					char cccc = bufferCharQueue.peekChar();
					if (cccc == '/') {
						bufferCharQueue.nextChar();
						return new RawToken(stringBuilder.toString(), RawTokenType.NOTES);
					}
				}
				stringBuilder.append(ccc);
			}
		}
	}
}
